﻿using Blog.Core.Domain.Model;
using Blog.Core.DTO;
using Blog.Core.DTO.ViewModel;
using Blog.Core.DTO.ViewModel.From;
using Blog.Core.IServices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
namespace Blog.Core.Api.Controllers
{
    /// <summary>
    /// 表单控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class FromController : BaseController<FromInfo>
    {
        /// <summary>
        ///依赖注入
        /// </summary>
        private readonly IFromService _fromService;
        private readonly IMapper _mapper;
        public FromController(IBaseService<FromInfo> baseService, IFromService fromService, IMapper mapper) : base(baseService)
        {
            _fromService = fromService;
            _mapper = mapper;
        }
        /// <summary>
        /// 表单的添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> FromInsert(RequestFromDTO model)
        {
            //统一返回值
            ResponseResultDTO<dynamic> result = new ResponseResultDTO<dynamic>();
            if(model==null)
            {
                result.Code = ResultStatusCode.PARAMSERROR;
                result.Message = "参数错误！！";
                return Ok(result);
            }
            int h =await _fromService.InsertFrom(model);
            //判断是否添加成功
            if (h>0)
            {
                result.Code = ResultStatusCode.SUCCESS;
                result.Message = "表单创建成功";
                return Ok(result);
            }
            else
            {
                result.Code = ResultStatusCode.HANDLEFAIL;
                result.Message = "表单创建失败";
                return Ok(result);
            }
        }
        /// <summary>
        /// 表单根据id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("ById")]
        public async Task<IActionResult> GetById(int id=0)
        {
            //统一返回值
            ResponseResultDTO<dynamic> result = new ResponseResultDTO<dynamic>();
            if (id==0)
            {
                result.Code = ResultStatusCode.PARAMSERROR;
                result.Message = "参数错误！！";
                return Ok(result);
            }
            var list =await  _fromService.GetById(id);
            result.Code = ResultStatusCode.SUCCESS;
            result.Message = "操作成功";
            result.Data = list;
            return Ok(result);
        }
        /// <summary>
        /// 表单的修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> FromUpdate(RequestFromDTO model)
        {
            //统一返回值
            ResponseResultDTO<dynamic> result = new ResponseResultDTO<dynamic>();
            if (model == null)
            {
                result.Code = ResultStatusCode.PARAMSERROR;
                result.Message = "参数错误！！";
                return Ok(result);
            }
            int h = await _fromService.UpdateFrom(model);
            //判断是否添加成功
            if (h > 0)
            {
                result.Code = ResultStatusCode.SUCCESS;
                result.Message = "表单修改成功";
                return Ok(result);
            }
            else
            {
                result.Code = ResultStatusCode.HANDLEFAIL;
                result.Message = "表单修改失败";
                return Ok(result);
            }
        }
        /// <summary>
        /// 表单的显示
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetFrom()
        {
            //统一返回值
            ResponseResultDTO<dynamic> result = new ResponseResultDTO<dynamic>();
            
            var list=await _fromService.GetAll();
            //判断是否添加成功
            if (list!=null)
            {
                result.Code = ResultStatusCode.SUCCESS;
                result.Message = "请求成功";
                result.Data = list;
                return Ok(result);
            }
            else
            {
                result.Code = ResultStatusCode.HANDLEFAIL;
                result.Message = "请求失败";
                return Ok(result);
            }
        }
        /// <summary>
        /// 表单的删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> FromDelete(int id=0)
        {
            //统一返回值
            ResponseResultDTO<dynamic> result = new ResponseResultDTO<dynamic>();
            if (id == 0)
            {
                result.Code = ResultStatusCode.PARAMSERROR;
                result.Message = "参数错误！！";
                return Ok(result);
            }
            var entity = await _fromService.GetById(id);
            if (entity==null)
            {
                result.Code = ResultStatusCode.HANDLEFAIL;
                result.Message = "操作失败！！";
                return Ok(result);
            }
            else
            {
                entity.Delete = 1;
               int h= await _fromService.Update(entity);
                if(h > 0)
                {
                    result.Code = ResultStatusCode.SUCCESS;
                    result.Message = "操作成功！！";
                    return Ok(result);
                }
                else
                {
                    result.Code = ResultStatusCode.HANDLEFAIL;
                    result.Message = "操作成功！！";
                    return Ok(result);
                }
            }
            
        }
    }
}
